package com.mtw.bbs.message.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mtw.bbs.common.core.constant.MessageConstant;

import com.mtw.bbs.common.core.security.UserDetails;
import com.mtw.bbs.common.core.security.context.SecurityContextHolder;
import com.mtw.bbs.common.core.vo.Result;
import com.mtw.bbs.message.pojo.entity.Message;
import com.mtw.bbs.message.pojo.vo.MessageVo;
import com.mtw.bbs.message.service.MessageService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;


/**
 * 消息
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("message")
public class MessageController {


    private final MessageService messageService;


    /**
     * 分页查询当前用户的消息
     * @param page              当前页
     * @param size              分页大小
     * @param type              消息类型： 1全部，2点赞，3回复
     */
    @GetMapping("/list")
    public Result messageList(@RequestParam(name = "page") int page,
                             @RequestParam(name = "size") int size,
                             @RequestParam(name = "type",required = false,defaultValue = "1") Integer type) {

        UserDetails user = SecurityContextHolder.getContext().getAuthentication();
        if (user == null){
            return Result.fail("请登录");
        }
        QueryWrapper<Message> qw = new QueryWrapper<>();
        if (type == 2){
            qw.lambda().in(Message::getType, MessageConstant.MSG_LIKE_ARTICLE,MessageConstant.MSG_LIKE_COMMENT);
        }
        if (type == 3){
            qw.lambda().in(Message::getType, MessageConstant.MSG_COMMENT_ARTICLE,MessageConstant.MSG_COMMENT_COMMENT);
        }
        qw.lambda().eq(Message::getReceiver,user.getId());
        qw.lambda().orderByAsc(Message::getReadStatus);
        qw.lambda().orderByDesc(Message::getCreatedDate);

        Page<MessageVo> voPage = messageService.getMessagVoPage(page, size, qw);
        return Result.success(voPage);
    }



    /**
     * 消息已读
     * @param id              消息id
     */
    @PutMapping("/read/{id}")
    public Result readMessage(@PathVariable String id) {

        UserDetails user = SecurityContextHolder.getContext().getAuthentication();
        if (user == null){
            return Result.fail("请登录");
        }
        Message message = new Message();
        message.setId(id);
        message.setReceiver(user.getId());

        boolean b = messageService.readMessage(message);
        return Result.success();
    }



    /**
     * 删除消息
     * @param id              消息id
     */
    @DeleteMapping("/delete/{id}")
    public Result messageList(@PathVariable String id) {

        UserDetails user = SecurityContextHolder.getContext().getAuthentication();
        if (user == null){
            return Result.fail("请登录");
        }
        Message message = new Message();
        message.setId(id);
        message.setReceiver(user.getId());

        boolean result = messageService.deleteMessage(message);
        return result?Result.success():Result.fail("删除失败");
    }

}
